package cn.springcloud.api.apiUtils;

import com.google.common.base.Joiner;

import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Random;

public class NumberUtil {

    private final static String numStr = "0123456789" ;

    private static int totalCount = 0;
    private static int selfIncreasingID;

    /**
     * 服务不停的情况下自增id，（否则请检测数据库表的数据进行初始化，不建议用于数据库）
     * @return
     */
    public static String selfIncreasingID() {
        ++totalCount;
        selfIncreasingID = totalCount;
        //输出结果 00000001
        return  new DecimalFormat("00000000").format(selfIncreasingID);
    }



    public static String getLengthNum(int length) {
        if(length<=0){
            length = 10 ;
        }
        HashSet<String> strSet = new HashSet<>(length);
        while(strSet.size()<length){
            strSet.add(String.valueOf(numStr.charAt(new Random().nextInt(numStr.length()))));
        }
        return Joiner.on("").join(strSet);

    }


    public static void main(String[] args) {

        System.out.println(NumberUtil.selfIncreasingID());
        System.out.println(NumberUtil.selfIncreasingID());
    }


}
